<template>
    <transition name="move">
        <div class="food" v-show="isShow" ref="food">
            <div class="food-content">
                <div class="image-header">
                    <img :src="food.image">
                    <div class="back" @click="back">
                        <i class="icon-arrow_lift"></i>
                    </div>
                </div>
                <div class="content">
                    <h2 class="name">{{food.name}}</h2>
                    <div class="extra">
                        <span class="count">月售{{food.sellCount}}份</span>
                        <span class="rating">好评率{{food.rating}}%</span>
                    </div>
                    <div class="price">
                        <span class="now">￥{{food.price}}</span>
                        <span v-if="food.oldPrice" class="old">￥{{food.oldPrice}}</span>
                    </div>
                    <div class="cartcontrol-wrapper">
                        <cart-control @add="addFood" :food="food"></cart-control>
                    </div>
                    <transition name="fade">
                        <div v-show="!food.count" class="buy" @click.stop.prevent="addFoodFirst">加入购物车</div>
                    </transition>
                </div>
                <v-split></v-split>
                <div class="info" v-show="food.info">
                    <h2 class="title">商品信息</h2>
                    <p class="info-content">{{food.info}}</p>
                </div>
                <v-split v-show="food.info"></v-split>
                <div class="rating">
                    <h2 class="title">商品评价</h2>
                    <v-rating-select @select="selectRating" @toggle="toggleContent" :onlyContent="onlyContent" :selectType="selectType" :ratings="food.ratings" :desc="desc"></v-rating-select>
                    <div class="rating-wrapper">
                        <ul v-show="food.ratings && food.ratings.length !== 0">
                            <li class="rating-item border-1px" v-show="needShow(rating.rateType, rating.text)" v-for="(rating, index) in food.ratings" :key="index">
                                <div class="user">
                                    <span class="name">{{rating.username}}</span>
                                    <img class="avatar" :src="rating.avatar" width="12px" height="12px">
                                </div>
                                <div class="time">{{rating.rateTime | formatDate}}</div>
                                <p class="text">
                                    <span :class="{'icon-thumb_up': rating.rateType===0, 'icon-thumb_down':rating.rateType===1}"></span>{{rating.text}}
                                </p>
                            </li>
                        </ul>
                        <div v-show="!food.ratings || !food.ratings.length" class="no-rating">暂无评价</div>
                    </div>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
import CartControl from '../cartcontrol/cartcontrol';
import vSplit from '../split/split';
import vRatingSelect from '../ratingselect/ratingselect';
import BScroll from 'better-scroll';
import Vue from 'vue';
import { formatDate } from '../../common/js/data.js';

const ALL = 2;

export default {
    name: 'food',
    props: {
        food: {
            type: Object
        },
        visible: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            isShow: false,
            desc: {
                all: '全部',
                negative: '吐槽',
                positive: '推荐'
            },
            selectType: ALL,
            onlyContent: true
        };
    },
    watch: {
        visible(value) {
            this.isShow = value;
            this.selectType = ALL;
            this.onlyContent = true;
            if (value === true) {
                this.$nextTick(() => {
                    if (!this.scroll) {
                        this.scroll = new BScroll(this.$refs.food, {
                            click: true
                        });
                    } else {
                        this.scroll.refresh();
                    }
                });
            }
        }
    },
    filters: {
        formatDate(time) {
            let date = new Date(time);
            return formatDate(date, 'yyyy-MM-dd hh:mm');
        }
    },
    methods: {
        back() {
            this.isShow = false;
            this.$emit('update:visible', this.isShow);
        },
        addFoodFirst(event) {
            if (!event._constructed) {
                return;
            }
            this.$emit('add', event.target);
            Vue.set(this.food, 'count', 1);
        },
        addFood(target) {
            this.$emit('add', target);
        },
        selectRating(type) {
            this.selectType = type;
            this.$nextTick(() => {
                this.scroll.refresh();
            });
        },
        toggleContent() {
            this.onlyContent = !this.onlyContent;
            this.$nextTick(() => {
                this.scroll.refresh();
            });
        },
        needShow(type, text) {

            if (this.onlyContent && !text) {
                return false;
            }

            if (this.selectType === ALL) {
                return true;
            } else {
                return this.selectType === type;
            }
        }
    },
    components: {
        CartControl,
        vSplit,
        vRatingSelect
    }
}
</script>

<style lang="less" scoped>
@import "../../common/styles/mixin.less";

.food {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 48px;
    width: 100%;
    z-index: 48;
    overflow: hidden;
    background: #fff;
    transform: translate3d(0, 0, 0);
    &.move-enter-active,
    &.move-leave-active {
        transition: all 0.5s;
    }

    &.move-enter,
    &.move-leave-to {
        transform: translate3d(100%, 0, 0);
    }
    .food-content {
        .image-header {
            position: relative;
            width: 100%;
            height: 0;
            padding-top: 100%;
            img {
                position: absolute;
                width: 100%;
                height: 100%;
                top: 0;
                left: 0;
            }
            .back {
                position: absolute;
                top: 10px;
                left: 0;

                .icon-arrow_lift {
                    display: block;
                    padding: 10px;
                    font-size: 20px;
                    color: #fff;
                }
            }
        }

        .content {
            padding: 18px;
            font-size: 0;
            position: relative;
            .name {
                font-size: 14px;
                font-weight: 700;
                color: rgb(7, 17, 27);
                line-height: 14px;
                margin-bottom: 8px;
            }
            .extra {
                color: rgb(147, 153, 159);
                margin: 8px 0 18px 0;
                .count {
                    font-size: 10px;
                    line-height: 10px;
                    margin-right: 12px;
                }
                .rating {
                    font-size: 10px;
                    line-height: 10px;
                }
            }
            .price {
                .now {
                    font-size: 14px;
                    font-weight: 700;
                    line-height: 24px;
                    color: rgb(240, 20, 20);
                    margin-right: 8px;
                }
                .old {
                    font-size: 10px;
                    font-weight: 700;
                    line-height: 24px;
                    color: rgb(147, 153, 159);
                    text-decoration: line-through;
                }
            }
            .cartcontrol-wrapper {
                position: absolute;
                bottom: 12px;
                right: 12px;
            }
            .buy {
                position: absolute;
                bottom: 18px;
                right: 18px;
                padding: 6px 12px;
                font-size: 10px;
                color: #fff;
                line-height: 12px;
                background: rgb(0, 160, 220);
                border-radius: 12px;
                opacity: 1;
                z-index: 10;
                &.fade-enter-active,
                &.fade-leave-active {
                    transition: all 0.5s;
                }
                &.fade-enter,
                &.fade-leave-to {
                    opacity: 0;
                    z-index: -1;
                }
            }
        }

        .info {
            padding: 18px;
            font-size: 0;
            .title {
                font-size: 14px;
                line-height: 14px;
                color: #07111b;
                margin-bottom: 6px;
            }
            .info-content {
                font-size: 12px;
                font-weight: 200;
                line-height: 24px;
                color: rgb(77, 85, 93);
                padding: 0 8px;
            }
        }
    }

    .rating {
        margin-top: 18px;
        .title {
            line-height: 14px;
            margin-left: 18px;
            font-size: 14px;
            color: #07111b;
        }

        .rating-wrapper {
            .no-rating {
                margin: 0 18px;
                padding: 16px 0;
                font-size: 12px;
                color: #93999f;
            }
            .rating-item {
                margin: 0 18px;
                padding: 16px 0;
                .border-1px(rgba(7, 17, 27, 0.1));
                font-size: 0;
                .user {

                    position: absolute;
                    right: 0;
                    top: 16px;
                    .name {
                        display: inline-block;
                        vertical-align: top;
                        font-size: 10px;
                        color: rgb(147, 153, 159);
                        line-height: 12px;
                        margin-right: 6px;
                    }
                    .avatar {
                        display: inline-block;
                        border-radius: 50%;
                    }
                }

                .time {
                    font-size: 10px;
                    color: rgb(147, 153, 159);
                    line-height: 12px;
                    margin-bottom: 6px;
                }

                .text {
                    font-size: 12px;
                    color: rgb(7, 17, 27);
                    line-height: 16px;

                    .icon-thumb_up {
                        color: rgb(0, 160, 220);
                        line-height: 24px;
                        margin-right: 4px;
                    }

                    .icon-thumb_down {
                        color: rgb(147, 153, 159);
                        line-height: 24px;
                        margin-right: 4px;
                    }
                }
            }
        }
    }
}
</style>
